package net.tngou.iask.action.my;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.entity.Td;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Cure;
import net.tngou.iask.pojo.Filter;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.reflect.Field;
import net.tngou.iask.reflect.FilterFun;
import net.tngou.iask.reflect.Method;
import net.tngou.iask.service.AccessService;
import net.tngou.iask.service.AnswerService;
import net.tngou.iask.service.PatientService;
import net.tngou.iask.service.QuestionService;
import net.tngou.iask.service.SurveyService;
import net.tngou.iask.util.IDUtil;

/**
 * 访问记录
 * @author tngou@tngou.net
 *
 */
public class AccessAction extends BaseAction {

	
	public void list() {
		if(user.getRole()!=3&&user.getRole()==4&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}
		int type = (int) ask.getId();
		AccessService service = new AccessService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), type, ask.getKeyword());
		root.put("page", page);
		root.put("type", type);
		root.put("IDUtil", new IDUtil());
		printFreemarker("my/access_list.ftl", root);
	}
	
	/**
	 * 
	 */
	public void ask() {
		
		long id=(long) ask.getId();
		if(id==0)
		{
			run_404();return;
		}
		Access access = new Access().get(id);
		if(access==null)
		{
			run_500();return;
		}
		Survey survey = new Survey().get(access.getSurvey());
		Patient patient = new Patient().get(access.getPatient());
		if(access.getCure()!=0)
		{
			Cure cure = new Cure().get(access.getCure());
			root.put("cure", cure);
		}
		access.setOpencount(access.getOpencount()+1);//更新次数
		access.update();
		root.put("access", access);
		root.put("survey", survey);
		root.put("patient", patient);	
		int state = access.getState();
		switch (state) {
		case 0://最新创建
			
			printFreemarker("my/access_start.ftl", root);
			break;
		case 1://访问进行中
			
			if(access.getAlive()==0)//生存访问
			{
				printFreemarker("my/access_alive.ftl", root);
			}
			if(access.getAlive()==-1)//死亡提示
			{
				printFreemarker("my/access_death.ftl", root);
			}
			if(access.getAlive()==1)
			{
				QuestionService questionService = new QuestionService();
				List<Question> list = questionService.getQuestion(survey.getId());
				Question question = new Question().get(access.getQuestion());
				if(question==null){run_500();return;}
				boolean ishide = _hide(access.getId(), question.getId(), patient.getId());
				while (ishide) {//过滤
					access.setSpeed(access.getSpeed()+1);
					for (int i = 0; i < list.size(); i++) {
						if(list.get(i).getId()==question.getId())
						{							
							if((i+1)==list.size())//完成
							{
								access.setQuestion(0);
								access.setState(2);//						
								access.setEndtime(new Timestamp(new Date().getTime()));//设置结束时间	
								printFreemarker("my/access_end.ftl", root);
								return;
							}else
							{
								access.setQuestion(list.get(i+1).getId());
							}
						}
					}
					access.update();
					question = new Question().get(access.getQuestion());
					ishide = _hide(access.getId(), question.getId(), patient.getId());
				}				
				List<Answer> answers = questionService.getAnswer(question.getId());
				root.put("question", question);
				root.put("list", answers);
				printFreemarker("my/access_ask.ftl", root);
			}
			break;
		case 2://访问完成
			
			printFreemarker("my/access_end.ftl", root);
			break;
		case -1://失效
			
			printFreemarker("my/access_error.ftl", root);
			break;
		default:
			break;
		}
		
		
	}
	
	/**
	 * 添加随访登记(个档案)
	 */
	@Deprecated
	public void healthadd() {
		if(user.getRole()!=3&&user.getRole()==4&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}

		List<?> surveys = service.getList("type="+1, Survey.class);//问卷列表
		List<Patient> patients=new ArrayList<>();
		int id=(int) ask.getId();		
		if(id!=0)
		{
			Patient patient = new Patient().get(id);
			if(patient!=null)patients.add(patient);
		}else
		{
			PatientService service = new PatientService();
			patients=service.list(user.getMain(),1);
		}	
		root.put("surveys", surveys);
		root.put("patients", patients);
	
		printFreemarker("my/access_healthadd.ftl", root);
	}
	
	/**
	 * 显示访问结果
	 */
	public void show() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		String[] params = request.getParams();
		int id=0;
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			id=Integer.parseInt(params[0]);		
		}
		if(id==0){run_404();return;}
		Access access = new Access().get(id);
		if(access==null){run_404();return;}
		Survey survey = new Survey().get(access.getSurvey());
		if(survey==null){run_404();return;}
		SurveyService service = new SurveyService();
		List<List<Td>> trs = service.getTrs(survey, access,true);
		root.put("trs", trs);
		root.put("survey", survey);
		root.put("access", access);
		printFreemarker("my/access_show.ftl", root);
	}
	
	/**
	 * 显示访问结果
	 */
	public void update() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		String[] params = request.getParams();
		int id=0;
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			id=Integer.parseInt(params[0]);		
		}
		if(id==0){run_404();return;}
		Access access = new Access().get(id);
		if(access==null){run_404();return;}
		Survey survey = new Survey().get(access.getSurvey());
		if(survey==null){run_404();return;}
		SurveyService service = new SurveyService();
		List<List<Td>> trs = service.getTrs(survey, access,false);
		root.put("trs", trs);
		root.put("survey", survey);
		root.put("access", access);
		printFreemarker("my/access_update.ftl", root);
	}
	
	
	/**
	 * 是否隐藏
	 * @param access
	 * @param question
	 * @param patient
	 * @return
	 */
	private boolean _hide(long access,long question,long patient)
	{
		QuestionService service = new QuestionService();
		List<Filter> list =  service.getFilter(question);
		if(list!=null&&list.size()>0)
		{			
			for (Filter filter : list) {
				String temp = filter.getTemp();
				Field field= new Field(access,question, patient);
				Method method = new Method(temp, field);
				Object r = method.run();
				if(r.equals(true)) return true;		
			}
		}
		return false;	
	}
}
